﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Xml;

namespace Viinikellari
{
    public static class XmlHelper
    {
        public static void Serialize<T>(T obj, string path)
        {
            using (var writer = new StreamWriter(path))
            {
                XmlSerializerNamespaces xns = new XmlSerializerNamespaces();
                xns.Add("", "");
                
                XmlTextWriter xmlWriter = new XmlTextWriter(writer);
                xmlWriter.Formatting = Formatting.Indented;

                XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
                xmlSerializer.Serialize(xmlWriter, obj, xns);
            }
        }

        public static T Deserialize<T>(string path)
        {
            object obj;

            using (var reader = new StreamReader(path))
            {
                XmlTextReader xmlReader = new XmlTextReader(reader);

                XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
                obj = xmlSerializer.Deserialize(xmlReader);
            }

            return (T)obj;
        }
    }
}
